package com.somiz.gamme;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import org.hibernate.Session;

import com.somiz.main.Control;
import com.somiz.main.Principal;
import com.tech.hibernate.HibernateUtil;

public class GammeForm extends JPanel {
	private JTextField txt1;
	private JTextField lab1;
	private JTextField txt2;
	private JTextField lab2;
	private JTextField txt3;
	private JTextField lab3;
	private JTextField txt4;
	private JTextField lab4;
	private JTextField txtnom;
	private static Long gammeId;
	private JButton button_2;

	/**
	 * Create the panel.
	 */
	public GammeForm() {
		setLayout(null);

		JButton button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (errForm().equals("")) {
					if (gammeId == null) {
						// creer
						Gamme gamme = new Gamme();
						gamme = FormeVersGamme(gamme);
						Session session = HibernateUtil.getSessionFactory()
								.openSession();
						if (Control.getGammeDao().isExisteAvecNom(gamme,
								session)) {
							JOptionPane
									.showMessageDialog(
											null,
											"cette gamme existe deja dans votre base de donnees1",
											"Erreur",
											JOptionPane.WARNING_MESSAGE);
						} else {
							Control.getGammeDao().Creer(gamme, session);
							gammeId = gamme.getId();
						}
						session.close();
					} else {
						// modif
						Session session = HibernateUtil.getSessionFactory()
								.openSession();
						Gamme gamme = new Gamme();
						gamme = FormeVersGamme(gamme);
						if (Control.getGammeDao().isExisteAvecNom(gamme,
								session)) {
							if (gammeId.equals(Control.getGammeDao()
									.TrouverAvecNom(gamme, session).getId())) {
								gamme = Control.getGammeDao().TrouverAvecId(
										gammeId, gamme, session);
								gamme = FormeVersGamme(gamme);
								Control.getGammeDao().modifier(gamme, session);
							} else {
								JOptionPane
										.showMessageDialog(
												null,
												"cette gamme existe deja dans votre base de donnees2",
												"Erreur",
												JOptionPane.WARNING_MESSAGE);
							}
						} else {
							gamme = Control.getGammeDao().TrouverAvecId(
									gammeId, gamme, session);
							gamme = FormeVersGamme(gamme);
							Control.getGammeDao().modifier(gamme, session);
						}
						session.close();
					}
				} else {
					JOptionPane.showMessageDialog(null, errForm(), "Erreur",
							JOptionPane.WARNING_MESSAGE);
				}

			}
		});
		button.setIcon(new ImageIcon(GammeForm.class
				.getResource("/cc/black/png/save_icon&32.png")));
		button.setPreferredSize(new Dimension(50, 50));
		button.setMnemonic(KeyEvent.VK_ENTER);
		button.setBackground(Color.WHITE);
		button.setBounds(10, 11, 50, 50);
		add(button);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 81, 393, 2);
		add(separator);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Gamme :",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 101, 393, 275);
		add(panel);

		txt1 = new JTextField();
		txt1.setColumns(10);
		txt1.setBounds(208, 71, 175, 34);
		panel.add(txt1);

		lab1 = new JTextField();
		lab1.setColumns(10);
		lab1.setBounds(10, 71, 175, 34);
		panel.add(lab1);

		txt2 = new JTextField();
		txt2.setColumns(10);
		txt2.setBounds(208, 116, 175, 34);
		panel.add(txt2);

		lab2 = new JTextField();
		lab2.setColumns(10);
		lab2.setBounds(10, 116, 175, 34);
		panel.add(lab2);

		txt3 = new JTextField();
		txt3.setColumns(10);
		txt3.setBounds(208, 161, 175, 34);
		panel.add(txt3);

		lab3 = new JTextField();
		lab3.setColumns(10);
		lab3.setBounds(10, 161, 175, 34);
		panel.add(lab3);

		txt4 = new JTextField();
		txt4.setColumns(10);
		txt4.setBounds(208, 204, 175, 34);
		panel.add(txt4);

		lab4 = new JTextField();
		lab4.setColumns(10);
		lab4.setBounds(10, 204, 175, 34);
		panel.add(lab4);

		txtnom = new JTextField();
		txtnom.setColumns(10);
		txtnom.setBounds(112, 26, 271, 34);
		panel.add(txtnom);

		JLabel lblNewLabel = new JLabel("Nom :");
		lblNewLabel.setBounds(10, 36, 92, 14);
		panel.add(lblNewLabel);

		button_2 = new JButton("");
		button_2.setIcon(new ImageIcon(GammeForm.class
				.getResource("/images/png/Add32.png")));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				Control.getGammeForm().vider();
				Control.getPrincipal()
						.getTabbedPane()
						.addTab("Gamme",
								new ImageIcon(
										Principal.class
												.getResource("/cc/black/png/cert_icon&16.png")),
								Control.getGammeForm(), null);
				Control.getPrincipal()
						.getTabbedPane()
						.setSelectedIndex(
								Control.getPrincipal().getTabbedPane()
										.getTabCount() - 1);
				session.close();
			}
		});
		button_2.setPreferredSize(new Dimension(50, 50));
		button_2.setMnemonic(KeyEvent.VK_ENTER);
		button_2.setBackground(Color.WHITE);
		button_2.setBounds(70, 11, 50, 50);
		add(button_2);
	}

	public void GammeVersForm(Gamme gamme) {
		txtnom.setText(gamme.getNom());
		txt1.setText(gamme.getDonn1());
		txt2.setText(gamme.getDonn2());
		txt3.setText(gamme.getDonn3());
		txt4.setText(gamme.getDonn4());
		lab1.setText(gamme.getLab1());
		lab2.setText(gamme.getLab2());
		lab3.setText(gamme.getLab3());
		lab4.setText(gamme.getLab4());
	}

	public Gamme FormeVersGamme(Gamme gamme) {
		gamme.setNom(txtnom.getText());
		gamme.setDonn1(txt1.getText());
		gamme.setDonn2(txt2.getText());
		gamme.setDonn3(txt3.getText());
		gamme.setDonn4(txt4.getText());
		gamme.setLab1(lab1.getText());
		gamme.setLab2(lab2.getText());
		gamme.setLab3(lab3.getText());
		gamme.setLab4(lab4.getText());
		return gamme;
	}

	public void vider() {
		txtnom.setText("");
		txt1.setText("");
		txt2.setText("");
		txt3.setText("");
		txt4.setText("");
		lab1.setText("");
		lab2.setText("");
		lab3.setText("");
		lab4.setText("");
		gammeId = null;
	}

	public String errForm() {
		String err = "";
		if (txtnom.getText().equals("")) {
			err = err + "il faut donner un nom pour la gamme\n";
		}
		return err;
	}

}
